package com.jsbs.iam.user.controller;

import com.jsbs.iam.common.core.dto.Result;
import com.jsbs.iam.common.core.utils.StringUtils;
import com.jsbs.iam.user.service.IApplySystemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Set;

/**
 * @Author junwc
 * @Date 2022/12/8
 * @Version 1.0
 */
@RestController
@RequestMapping("/iam/user/apply/system")
public class IamApplySystemController extends BaseController{
    private static final Logger logger = LoggerFactory.getLogger(IamApplySystemController.class);

    @Autowired
    private IApplySystemService applySystemService;

    /**
     * 功能描述：获取应用列表
     * @return
     */
    @PostMapping("/query")
    public Result applySystemQuery(HttpServletRequest httpServletRequest) {
        logger.info("Route==>IamApplySystemController method=applySystemQuery 查询应用列表");
        try {
            // 获取当前登录人归属租户
            // String companyCode = (String) httpServletRequest.getSession().getAttribute("companyCode");
            return Result.success(applySystemService.queryList("000001"));
        } catch (Exception e) {
            logger.error("Route==>IamMenuController method=menuQuery 查询应用列表异常：", e);
            return handleException(e);
        }
    }

    /**
     * 功能描述：根据域名获取应用信息
     * @return
     */
    @PostMapping("/getApplySystem")
    public Result getApplySystem(@RequestBody String requestUrl) {
        logger.info("Route==>IamApplySystemController method=getApplySystem 根据域名获取应用信息requestUrl：{}", requestUrl);
        try {
            return Result.success(applySystemService.queryByRequestUrl(requestUrl));
        } catch (Exception e) {
            logger.error("Route==>IamMenuController method=getApplySystem 根据域名获取应用信息异常：", e);
            return handleException(e);
        }
    }

    /**
     * 功能描述：根据应用类型获取应用列表下拉框
     * @return
     */
    @PostMapping("/applySystemList")
    public Result getApplySystemList(HttpServletRequest request, @RequestParam String applyTypeCode) {
        logger.info("Route==>IamApplySystemController method=getApplySystemList 根据应用类型获取应用列表applyTypeCode：{}", applyTypeCode);
        try {
            String companyCode = "000001"/*request.getHeader(Constants.COMPANY_CODE)*/;
            if (StringUtils.isEmpty(companyCode)) {
                return Result.failWithMsg("当前登录信息异常");
            }
            // 获取应用列表
            return Result.success(applySystemService.queryByTypeCode(companyCode, applyTypeCode));
        } catch (Exception e) {
            logger.error("Route==>IamMenuController method=getApplySystemList 根据域名获取应用信息异常：", e);
            return handleException(e);
        }
    }

    /**
     * 功能描述：根据应用编码查询应用相关信息
     * param：applyCodes
     * param：companyCode
     * @return Result
     */
    @PostMapping("/queryApplySystemByApplyCode")
    public Result queryApplySystemByApplyCode(@RequestParam Set<String> applyCodes, @RequestParam String companyCode) {
        try {
            if(CollectionUtils.isEmpty(applyCodes)){
                return Result.failWithMsg("应用编码不可为空！");
            }
            logger.info("Route==>IamApplySystemController method=getApplySystem 根据应用编码获取应用信息入参：{}", applyCodes);
            return Result.success(applySystemService.queryApplySystemByApplyCode(applyCodes, companyCode));
        } catch (Exception e) {
            logger.error("Route==>IamMenuController method=queryApplySystemByApplyCode 根据应用编码获取应用信息异常：", e);
            return handleException(e);
        }
    }


}
